/**
 * 
 */
package dms.model.role.wrapper;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import dms.dto.DormitoryChangeDTO;
import dms.dto.PurchaseItemDTO;
import dms.dto.RepairDTO;
import dms.dto.StudentDTO;
import dms.model.Building;
import dms.model.Dormitory;
import dms.model.exception.NoPermissionException;
import dms.model.exception.NoSuchEntityException;
import dms.model.exception.SuchOperationAreNotAllowedException;
import dms.model.form.DormitoryChange;
import dms.model.form.PurchaseItem;
import dms.model.form.Repair;
import dms.model.role.DormSupervisor;
import dms.model.role.Student;
import dms.model.util.PageIterator;
import dms.repository.BuildingRepository;
import dms.repository.DormitoryChangeRepository;
import dms.repository.DormitoryRepository;
import dms.repository.PurchaseItemRepository;
import dms.repository.RepairRepository;
import dms.repository.StudentRepository;
import dms.repository.SystemAdministratorRepository;
import dms.util.PasswordUtil;

/**
 * 学生的包装类,业务操作在该类中，实体数据在变量student中，
 * 该类的业务操作必须使用实体数据，所有使用前必须通过注入
 * 通过BeanFactory获得，否则产生NullPointerException
 * @author 李健
 *
 */
public class StudentWrapper {
	
	private Student student = null;
	
	//学生要用的仓储
	private RepairRepository repairRepository = null;
	private PurchaseItemRepository purchaseItemRepository = null;
	private DormitoryChangeRepository dormitoryChangeRepository = null;
	private SystemAdministratorRepository systemAdministratorRepository = null;
	private BuildingRepository buildingRepository = null;
	private DormitoryRepository dormitoryRepository = null;
	private StudentRepository studentRepository = null;
	
	/******************************************************************************************/
	// Domain operation 
	
	public Repair addRepair(RepairDTO repairInfo) throws Exception {
		Repair repair = new Repair();
		Building building = this.getStudent().getDormitory().getBuilding();
		Dormitory dormitory = this.getStudent().getDormitory();
		
		repair.setReason(repairInfo.getReason());
		repair.setBuilding(building);
		repair.setDormitory(dormitory);
		repair.setRequestDate(new Date());
		repair.setStudent(this.getStudent());
		repair.setTitle(repairInfo.getTitle());
		return this.repairRepository.createRepair(repair);
	}
	
	public Repair updateRepair(RepairDTO repairInfo) throws Exception {
		
		Repair repair = this.repairRepository.findById(repairInfo.getId());
		
		if(!repair.getStudent().getId().equals(this.getStudent().getId())) {
			throw new NoPermissionException("没有权限");
		}
		if (repair.getStatus().isProcessed()) {
			throw new SuchOperationAreNotAllowedException("表单已受理不能修改");
		}
		
		repair.setTitle(repairInfo.getTitle());
		repair.setReason(repairInfo.getReason());		
		return this.repairRepository.updateRepair(repair);
	}
	
	public void deleteRepairById(Long id) throws Exception {
		Repair repair = this.repairRepository.findById(id);
		if (!repair.getStudent().getId().equals(this.getStudent().getId())) {
			throw new NoPermissionException("不能删除，不是你申请的维修单");
		}
		if (repair.getStatus().isProcessed()) {
			throw new SuchOperationAreNotAllowedException("表单已受理不能删除");
		}
		
		this.repairRepository.deleteRepairById(id);
	}
	
	public void deleteRepairs(List<Long> ids) throws Exception {
		Iterator<Long> iter = ids.iterator();
		
		while (iter.hasNext()) {
			Long id = iter.next();
			Repair repair = this.repairRepository.findById(id);
			
			if (!repair.getStudent().getId().equals(this.getStudent().getId())) {
				throw new NoPermissionException("不能删除，不是你申请的维修单");
			}
			if (repair.getStatus().isProcessed()) {
				throw new SuchOperationAreNotAllowedException("表单已受理不能删除");
			}
		}
		
		this.repairRepository.deleteRepairs(ids);
	}
	/**
	 * 查找时间段的该生寝室repair
	 * @param fromDate
	 * @param toDate
	 * @return
	 * @deprecated
	 */
	public PageIterator<Repair> findRepair(Date fromDate, Date toDate) {
		return null;
	}
	/**
	 * 查找该生寝室所有repair
	 * @param fromDate
	 * @param toDate
	 * @return
	 * @deprecated
	 */
	public PageIterator<Repair> findRepair() {
		return findRepair(null, null);
	}
	
	public PurchaseItem addPurchaseItem(PurchaseItemDTO purchaseItemInfo) throws Exception {
		PurchaseItem purchaseItem = new PurchaseItem();
		
		purchaseItem.setContactTel(purchaseItemInfo.getContactTel());
		purchaseItem.setTitle(purchaseItemInfo.getTitle());
		purchaseItem.setDescription(purchaseItemInfo.getDescription());
		purchaseItem.setDormitory(this.getStudent().getDormitory());
		purchaseItem.setRequestDate(new Date());
		purchaseItem.setStudent(this.getStudent());
		
		return this.purchaseItemRepository.createPurchaseItem(purchaseItem);
		
	}
	
	public PurchaseItem updatePurchaseItem(PurchaseItemDTO purchaseItemInfo) throws Exception {
		PurchaseItem purchaseItem = this.purchaseItemRepository.findById(purchaseItemInfo.getId());
		
		if (!purchaseItem.getStudent().getId().equals(this.getStudent().getId())) {
			throw new NoPermissionException("你没有权限进行修改");
		}
		if (purchaseItem.getStatus().isProcessed()) {
			throw new SuchOperationAreNotAllowedException("购买申请已受理，不能修改");
		}
		
		purchaseItem.setContactTel(purchaseItemInfo.getContactTel());
		purchaseItem.setDescription(purchaseItemInfo.getDescription());
		purchaseItem.setTitle(purchaseItemInfo.getTitle());
		purchaseItem.setStudent(this.getStudent());
		
		return this.purchaseItemRepository.updatePurchaseItem(purchaseItem);
	}
	
	public void deletePurchaseItems(List<Long> ids) throws Exception {
		java.util.Iterator<Long> iter = ids.iterator();
		
		while (iter.hasNext()) {
			Long id = iter.next();
			PurchaseItem purchaseItem = this.purchaseItemRepository.findById(id);
			
			if (!purchaseItem.getStudent().getId().equals(this.getStudent().getId())) {
				throw new NoPermissionException("你没有权限进行修改");
			}
			if (purchaseItem.getStatus().isProcessed()) {
				throw new SuchOperationAreNotAllowedException("购买申请已受理，不能删除");
			}
		}
		
		this.purchaseItemRepository.deletePurchaseItems(ids);
	}
	
	public void deletePurchaseItemById(Long id) throws Exception {
		PurchaseItem purchaseItem = this.purchaseItemRepository.findById(id);
		
		if (!purchaseItem.getStudent().getId().equals(this.getStudent().getId())) {
			throw new NoPermissionException("你没有权限进行修改");
		}
		
		if (purchaseItem.getStatus().isProcessed()) {
			throw new SuchOperationAreNotAllowedException("购买申请已受理，不能删除");
		}
		
		this.purchaseItemRepository.deletePurchaseItemById(id);
	}
	/**
	 * @deprecated
	 */
	public PageIterator<PurchaseItem> findPurchaseItem(Date fromDate, Date toDate) {
		return null;
	}
	
	public DormitoryChange addDormitoryChange(DormitoryChangeDTO dormitoryChangeInfo) throws Exception {
		DormitoryChange dormitoryChange = new DormitoryChange();
		Dormitory dormitory = this.dormitoryRepository.findById(dormitoryChangeInfo.getDormitory());
		
		dormitoryChange.setDormitory(dormitory);
		dormitoryChange.setReason(dormitoryChangeInfo.getReason());
		dormitoryChange.setRequestDate(new Date());
		dormitoryChange.setStudent(this.getStudent());
		
		return this.dormitoryChangeRepository.createDormitoryChange(dormitoryChange);
	}
	
	public DormitoryChange updateDormitoryChange(DormitoryChangeDTO dormitoryChangeInfo) throws Exception {
		DormitoryChange dormitoryChange = this.dormitoryChangeRepository.findById(dormitoryChangeInfo.getId());
		Dormitory dormitory = this.dormitoryRepository.findById(dormitoryChangeInfo.getDormitory());
		
		if (!dormitoryChange.getStudent().getId().equals(this.getStudent().getId())) {
			throw new NoPermissionException("你没有权限修改该申请");
		}
		
		if (dormitoryChange.getStatus().isProcessed()) {
			throw new SuchOperationAreNotAllowedException("更换申请已受理，不能更新");
		}
		
		dormitoryChange.setDormitory(dormitory);
		dormitoryChange.setReason(dormitoryChangeInfo.getReason());
		dormitoryChange.setStudent(this.getStudent());
		
		return this.dormitoryChangeRepository.updateDormitoryChange(dormitoryChange);
	}
	
	public void deleteDormitoryChangeById(Long id) throws Exception {
		DormitoryChange dormitoryChange = this.dormitoryChangeRepository.findById(id);
		
		if (!dormitoryChange.getStudent().getId().equals(this.getStudent().getId())) {
			throw new NoPermissionException("你没有权限进行修改");
		}
		if (dormitoryChange.getStatus().isProcessed()) {
			throw new SuchOperationAreNotAllowedException("申请已受理，不能更新");
		}
		this.dormitoryChangeRepository.deleteDormitoryChangeById(id);
	}
	
	/**
	 * 
	 * @param ids
	 * @throws Exception
	 */
	public void deleteDormitoryChanges(List<Long> ids) throws Exception {
		Iterator<Long> iter = ids.iterator();
		
		while (iter.hasNext()) {
			Long id = iter.next();
			DormitoryChange dormitoryChange = this.dormitoryChangeRepository.findById(id);
			if (dormitoryChange == null) {
				throw new NoSuchEntityException("没有该实体");
			}
			if (!dormitoryChange.getStudent().getId().equals(this.getStudent().getId())) {
				throw new NoPermissionException("你没有权限进行修改");
			}
			if (dormitoryChange.getStatus().isProcessed()) {
				throw new SuchOperationAreNotAllowedException("申请已受理，不能更新");
			}
		}
		
		this.dormitoryChangeRepository.deleteDormitoryChanges(ids);
	}
	
	@Deprecated
	public PageIterator<DormitoryChange> findDormitoryChange(Date fromDate, Date toDate) {
		return null;
	}
	
	/**
	 * 学生修改自己信息，只修改邮件，或密码
	 * @param studentInfo
	 * @return
	 * @throws NoPermissionException 
	 */
	public Student modifyPersonalInfo(StudentDTO studentInfo) throws NoPermissionException {
	
		if (!studentInfo.getId().equals(this.getStudent().getId())) {
			throw new NoPermissionException("没有权限");
		}
		if (studentInfo.getEmail() != null 
				&& studentInfo.getOldEmail() != null
				&& studentInfo.getEmail().length() != 0 
				&& studentInfo.getOldEmail().length() != 0) {
			if (this.getStudent().getAccount().getEmail().equals(
					studentInfo.getOldEmail())) {
				this.getStudent().getAccount().setEmail(studentInfo.getEmail());
			} else {
				throw new NoPermissionException("原邮件错误，你没有权限修改邮件地址");
			}
		}
		
		if (studentInfo.getPassword() != null 
				&& studentInfo.getOldPassword() != null
				&& studentInfo.getPassword().length() != 0
				&& studentInfo.getOldPassword().length() != 0) {
			String oldPassword = PasswordUtil.MD5Digest(studentInfo.getOldPassword());
			if (this.getStudent().getAccount().getPassword().equals(
					oldPassword)) {
				this.getStudent().getAccount().setPassword(PasswordUtil.MD5Digest(studentInfo.getPassword()));
			} else {
				throw new NoPermissionException("原密码错误，你没有权限修改密码");
			}
		}
		
		if (studentInfo.getTelephone() != null) {
			this.getStudent().getAccount().setTelephone(studentInfo.getTelephone());
		}
		this.getStudent().getAccount().setModifyDate(new Date());
		return this.studentRepository.updateStudent(this.getStudent());
	}

	/**
	 * @return the student
	 */
	public Student getStudent() {
		return student;
	}

	/**
	 * @param student the student to set
	 */
	public void setStudent(Student student) {
		this.student = student;
	}

	/**
	 * @return the repairRepository
	 */
	public RepairRepository getRepairRepository() {
		return repairRepository;
	}

	/**
	 * @param repairRepository the repairRepository to set
	 */
	public void setRepairRepository(RepairRepository repairRepository) {
		this.repairRepository = repairRepository;
	}

	/**
	 * @return the purchaseItemRepository
	 */
	public PurchaseItemRepository getPurchaseItemRepository() {
		return purchaseItemRepository;
	}

	/**
	 * @param purchaseItemRepository the purchaseItemRepository to set
	 */
	public void setPurchaseItemRepository(
			PurchaseItemRepository purchaseItemRepository) {
		this.purchaseItemRepository = purchaseItemRepository;
	}

	/**
	 * @return the dormitoryChangeRepository
	 */
	public DormitoryChangeRepository getDormitoryChangeRepository() {
		return dormitoryChangeRepository;
	}

	/**
	 * @param dormitoryChangeRepository the dormitoryChangeRepository to set
	 */
	public void setDormitoryChangeRepository(
			DormitoryChangeRepository dormitoryChangeRepository) {
		this.dormitoryChangeRepository = dormitoryChangeRepository;
	}

	/**
	 * @return the systemAdministratorRepository
	 */
	public SystemAdministratorRepository getSystemAdministratorRepository() {
		return systemAdministratorRepository;
	}

	/**
	 * @param systemAdministratorRepository the systemAdministratorRepository to set
	 */
	public void setSystemAdministratorRepository(
			SystemAdministratorRepository systemAdministratorRepository) {
		this.systemAdministratorRepository = systemAdministratorRepository;
	}

	/**
	 * @return the buildingRepository
	 */
	public BuildingRepository getBuildingRepository() {
		return buildingRepository;
	}

	/**
	 * @param buildingRepository the buildingRepository to set
	 */
	public void setBuildingRepository(BuildingRepository buildingRepository) {
		this.buildingRepository = buildingRepository;
	}

	/**
	 * @return the dormitoryRepository
	 */
	public DormitoryRepository getDormitoryRepository() {
		return dormitoryRepository;
	}

	/**
	 * @param dormitoryRepository the dormitoryRepository to set
	 */
	public void setDormitoryRepository(DormitoryRepository dormitoryRepository) {
		this.dormitoryRepository = dormitoryRepository;
	}

	/**
	 * @return the studentRepository
	 */
	public StudentRepository getStudentRepository() {
		return studentRepository;
	}

	/**
	 * @param studentRepository the studentRepository to set
	 */
	public void setStudentRepository(StudentRepository studentRepository) {
		this.studentRepository = studentRepository;
	}

	/**
	 * @param repairRepository
	 * @param purchaseItemRepository
	 * @param dormitoryChangeRepository
	 * @param systemAdministratorRepository
	 * @param buildingRepository
	 * @param dormitoryRepository
	 * @param studentRepository
	 */
	public StudentWrapper(RepairRepository repairRepository,
			PurchaseItemRepository purchaseItemRepository,
			DormitoryChangeRepository dormitoryChangeRepository,
			SystemAdministratorRepository systemAdministratorRepository,
			BuildingRepository buildingRepository,
			DormitoryRepository dormitoryRepository,
			StudentRepository studentRepository) {
		super();
		this.repairRepository = repairRepository;
		this.purchaseItemRepository = purchaseItemRepository;
		this.dormitoryChangeRepository = dormitoryChangeRepository;
		this.systemAdministratorRepository = systemAdministratorRepository;
		this.buildingRepository = buildingRepository;
		this.dormitoryRepository = dormitoryRepository;
		this.studentRepository = studentRepository;
	}

	/**
	 * 
	 */
	public StudentWrapper() {
		super();
	}
	
}
